//
//  XKLLaunchViewController.m
//  GuiZiShouDuanZi
//
//  Created by xmg on 16/4/27.
//  Copyright © 2016年 xiao_kl. All rights reserved.
//

#import "XKLLaunchViewController.h"
#import <AFNetworking.h>
#import "XKLADItem.h"
#import <MJExtension.h>
#import <UIImageView+WebCache.h>
#import "XKLTabBarController.h"
#import "XKLPushGiudeView.h"
#define Iphone6P [UIScreen mainScreen].bounds.size.height==736
#define Iphone6 [UIScreen mainScreen].bounds.size.height==667
#define Iphone5 [UIScreen mainScreen].bounds.size.height==568
#define Iphone4 [UIScreen mainScreen].bounds.size.height==480


#define TYPE @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"

@interface XKLLaunchViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *launchIMageView;
@property (weak, nonatomic)UIImageView *DAImageView;
@property (weak, nonatomic) IBOutlet UIButton *But;

@property (nonatomic,strong)XKLADItem *ADitem;
@property (nonatomic,strong)NSTimer *timer;
@end

@implementation XKLLaunchViewController

-(UIImageView *)DAImageView{
    if (_DAImageView==nil) {
        UIImageView *image=[[UIImageView alloc] init];
        
        _DAImageView=image;
        [self.launchIMageView addSubview:image];
        self.launchIMageView.userInteractionEnabled=YES;
        _DAImageView.userInteractionEnabled=YES;
        //添加手势
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGes:)];
        [_DAImageView addGestureRecognizer:tap];
    }
    return _DAImageView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self upLanchImage];
    
    [self sentSession];
    
    //读秒
    self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerEvent) userInfo:nil repeats:YES];
    
}

//点击跳转到广告界面
-(void)tapGes:(UITapGestureRecognizer *)tap{
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:self.ADitem.ori_curl]]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.ADitem.ori_curl]];
    }
}

//点击跳入应用
- (IBAction)butClick:(id)sender {
    XKLTabBarController *tab=[[XKLTabBarController alloc] init];
    [UIApplication sharedApplication].keyWindow.rootViewController=tab;
    [XKLPushGiudeView show];
    //释放定时器
    [self.timer invalidate];
}

-(void)timerEvent{
    
    static NSInteger index=3;
    index-=1;
    if (index<0) {
        XKLTabBarController *tab=[[XKLTabBarController alloc] init];
        [UIApplication sharedApplication].keyWindow.rootViewController=tab;
        [XKLPushGiudeView show];
        //释放定时器
        [self.timer invalidate];
    }
    [self.But setTitle:[NSString stringWithFormat:@"跳转(%ld)",index] forState:UIControlStateNormal];
}
-(void)upLanchImage{
    
    if (Iphone6P) {
        self.launchIMageView.image=[UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
    }else if (Iphone6){
        self.launchIMageView.image=[UIImage imageNamed:@"LaunchImage-800-667h@2x"];
    }else if (Iphone5){
        self.launchIMageView.image=[UIImage imageNamed:@"LaunchImage-568h@2x"];
    }else if (Iphone4){
        self.launchIMageView.image=[UIImage imageNamed:@"LaunchImage"];
    }
}

-(void)sentSession{
    NSMutableDictionary *dic=[NSMutableDictionary dictionary];
    dic[@"code2"]=TYPE;
    
    [[AFHTTPSessionManager manager] GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic=[responseObject[@"ad"] firstObject];
        XKLADItem *item=[XKLADItem mj_objectWithKeyValues:dic];
        self.ADitem=item;
        if (item.w) {
            self.DAImageView.frame=CGRectMake(0, 0,ScreenW,ScreenW/item.w *item.h);
        }else{
            self.DAImageView.frame=CGRectZero;
        }
        
        //请求图片
        [self.DAImageView sd_setImageWithURL:[NSURL URLWithString:item.w_picurl]];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}


-(BOOL)prefersStatusBarHidden{
    return YES;
}



@end
